package com.javakc.copyrightin.bookList.controller;

import com.alibaba.excel.EasyExcel;
import com.javakc.commonutils.api.APICODE;
import com.javakc.copyrightin.bookList.entity.BookList;
import com.javakc.copyrightin.bookList.listener.BookListListener;
import com.javakc.copyrightin.bookList.service.BookListService;
import com.javakc.copyrightin.bookList.vo.BookListData;
import com.javakc.copyrightin.bookList.vo.BookListQuery;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@Api(tags = "书单管理")
@RestController
@RequestMapping("/copyrightin/bookList")
@CrossOrigin
public class BookListController {
    @Autowired
    private BookListService bookListService;

    @ApiOperation(value = "查询所有书单")
    @GetMapping
    public APICODE findAll() {
        List<BookList> list = bookListService.findAll();
        return APICODE.OK().data("items", list);
    }

    @ApiOperation("带分页的条件查询")
    @PostMapping("{pageNo}/{pageSize}")
    public APICODE findPage(
            @ApiParam(name = "pageNo", value = "当前页码", required = true) @PathVariable("pageNo") int pageNo,
            @ApiParam(name = "pageSize", value = "每页显示的最大条数", required = true) @PathVariable("pageSize") int pageSize,
            @ApiParam(name = "bookQuery", value = "查询条件封装对象", required = false) @RequestBody(required = false) BookListQuery bookListQuery) {
        Page<BookList> page = bookListService.findPage(pageNo, pageSize, bookListQuery);
        List<BookList> list = page.getContent();
        long totalElements = page.getTotalElements();
        return APICODE.OK().data("items", list).data("total", totalElements);
    }

    @ApiOperation(value = "新增书单")
    @PostMapping("saveBookList")
    public APICODE saveBookList(@RequestBody BookList bookList) {
        bookListService.saveOrUpdate(bookList);
        return APICODE.OK().data("bookList", bookList);
    }

    @ApiOperation(value = "修改书单")
    @PutMapping("updateBookList")
    public APICODE updateBookList(@RequestBody BookList bookList) {
        bookListService.saveOrUpdate(bookList);
        return APICODE.OK().data("bookList", bookList);
    }

    @ApiOperation(value = "根据ID获取书单")
    @GetMapping("{id}")
    public APICODE view(@PathVariable("id") Integer id) {
        BookList bookList = bookListService.getById(id);
        return APICODE.OK().data("bookList", bookList);
    }

    @ApiOperation(value = "根据ID删除书单")
    @DeleteMapping("{id}")
    public APICODE deleteBookList(@PathVariable Integer id) {
        bookListService.removeById(id);
        return APICODE.OK();
    }

    //导入
    @ApiOperation(value = "列表导入", notes = "使用阿里EasyExcel导入Excel格式的用户列表数据")
    @PostMapping("importEasyExcel")
    public void importEasyExcel(MultipartFile file) {
        try {
            EasyExcel.read(file.getInputStream(), BookListData.class, new BookListListener(bookListService)).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
